package net.hanjava.swing;

import static java.awt.AlphaComposite.SRC_OVER;
import static java.awt.Color.BLACK;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 * transcluent using <code>AlphaComposite</code>
 * @author behumble
 */
@SuppressWarnings("serial")
public class Alpha1 extends JLabel {
	public static final String URL_ICON = "http://lh3.google.co.kr/behumble/R7RpD1W3pwI/AAAAAAAAAvY/VoaM1l7R8Ec/SANY0033.JPG.jpg?imgmax=576";

	public Alpha1(String imgurl) throws MalformedURLException {
		URL url = new URL(imgurl);
		ImageIcon icon = new ImageIcon(url);
		setIcon(icon);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		AlphaComposite alpha = AlphaComposite.getInstance(SRC_OVER, 0.5f);
		Graphics2D g2 = (Graphics2D) g;
		g2.setComposite(alpha);
		g.setColor(BLACK);
		int w = getWidth();
		int h = getHeight();
		g.fillRect(0, 0, w / 2, h);
	}

	public static void main(String[] args) throws MalformedURLException {
		JLabel label = new Alpha1(URL_ICON);
		Util.show(label);
	}
}